/**
 * Definition for singly-linked list.**/
struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution
{
public:
    ListNode *removeNthFromEnd(ListNode *head, int n)
    {
        auto p = head;
        int cnt = 0;
        while (p)
        {
            p = p->next;
            cnt++;
        }
        p = head;
        ListNode *p_pre = nullptr;
        int i = 0;
        while (p)
        {
            if (i == cnt - n)
            {
                if (p_pre)
                {
                    p_pre->next = p->next;
                    delete p;
                    return head;
                }
                else
                {
                    head == p->next;
                    return head;
                }
            }
            p_pre = p;
            p = p->next;
            i++;
        }
        return head;
    }
};

void main()
{
    auto p = new ListNode(1);
    auto b = Solution().removeNthFromEnd(p, 1);
}